home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_ATxgopher.idb / usr / freeware / src / xgopher.1.3 / item.c.z / item.c
C/C++ Source or Header  |  1998-01-21  |  5KB  |  202 lines

  1. /* item.c
  2.    routines to manage the data fields for gopher items */
  3.  
  4.      /*---------------------------------------------------------------*/
  5.      /* Xgopher        version 1.3     08 April 1993                  */
  6.      /*                version 1.2     20 November 1992               */
  7.      /*                version 1.1     20 April 1992                  */
  8.      /*                version 1.0     04 March 1992                  */
  9.      /* X window system client for the University of Minnesota        */
  10.      /*                                Internet Gopher System.        */
  11.      /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
  12.      /*                Computing and Communications Services Office   */
  13.      /* Copyright 1992, 1993 by                                       */
  14.      /*           the Board of Trustees of the University of Illinois */
  15.      /* Permission is granted to freely copy and redistribute this    */
  16.      /* software with the copyright notice intact.                    */
  17.      /*---------------------------------------------------------------*/
  18.  
  19.  
  20. #include <stdio.h>
  21.  
  22. #include "gopher.h"
  23. #include "item.h"
  24. #include "itemList.h"
  25. #include "osdep.h"
  26.  
  27.  
  28.  
  29. /* Item data management routines:
  30.     newItem()            return ptr to a gopherItem structure
  31.     copyItem(gopherItemP)        return pointer to a copy of an item
  32.     freeItem(gopherItemP)        free the contents of item and release
  33.     compareItem(gopherItemP, gopherItemP) T/F; item contents are equal
  34.     initItemFields(gopherItemP)    init fields of freshly allocated items
  35.  
  36.     getItemIndex(gopherItemP)    return an item's index search string
  37.  
  38.    debugging routines:
  39.     printItem            print contents of an item
  40. */
  41.  
  42.  
  43. /* newItem 
  44.     Return a fresh empty gopher item */
  45.  
  46. gopherItemP
  47. newItem()
  48. {
  49.     gopherItemP    gi;
  50.  
  51.     gi = acquireItem();
  52.  
  53.     gi->type = '\0';
  54.     gi->plus = FALSE;
  55.     gi->sc     = (scInfo *) NULL;
  56.     gi->accessOk    = FALSE;
  57.  
  58.     return gi;
  59. }
  60.  
  61.  
  62. /* copyItem 
  63.     return a copy of a gopher item */
  64.  
  65. gopherItemP
  66. copyItem(old_gi)
  67. gopherItemP    old_gi;
  68. {
  69.     gopherItemP    gi;
  70.  
  71.     if (old_gi == NULL) return NULL;
  72.  
  73.     gi = acquireItem();
  74.     gi->type = old_gi->type;
  75.     strncpy(USER_STRING_PREFIX(gi), USER_STRING_PREFIX(old_gi), PREFIX_LEN);
  76.     strncpy(USER_STRING(gi), USER_STRING(old_gi), USER_STRING_LEN);
  77.     vStringSet(&(gi->selector), vStringValue(&(old_gi->selector)));
  78.     strncpy(gi->host, old_gi->host, HOST_STRING_LEN);
  79.     gi->port = old_gi->port;
  80.     gi->plus = old_gi->plus;
  81.     gi->sc     = old_gi->sc;
  82.     gi->accessOk    = old_gi->accessOk;
  83.  
  84.     return gi;
  85. }
  86.  
  87.  
  88. /* freeItem 
  89.     Free the contents of a gopher item */
  90.  
  91. void
  92. freeItem(gi)
  93. gopherItemP    gi;
  94. {
  95.     if (gi != NULL) {
  96.  
  97.         /* most of the gopher item is static, so there is no
  98.            point to freeing anything.  Except the selector string.
  99.            For now, it is sufficient to NOT free the selector
  100.            however, since it will be re-used in place if possible
  101.            when the item is reused. */
  102.  
  103.         releaseItem(gi);
  104.     }
  105.  
  106.     return;
  107. }
  108.  
  109.  
  110. /* compareItem 
  111.     return boolean for two gopher items having equal content */
  112.  
  113. BOOLEAN
  114. compareItem(g1, g2)
  115. gopherItemP    g1, g2;
  116. {
  117.     if ( g1->type != g2->type )                return FALSE;
  118.     if ( strcmp(USER_STRING(g1), USER_STRING(g2)) != 0 )     return FALSE;
  119.     if ( strcmp(vStringValue(&(g1->selector)),
  120.             vStringValue(&(g2->selector))) != 0 )     return FALSE;
  121.     if ( strcmp(g1->host, g2->host) != 0 )             return FALSE;
  122.     if ( g1->port != g2->port )                 return FALSE;
  123.  
  124. #ifdef NOTYET
  125.     if ( g1->plus != g2->plus )                 return FALSE;
  126. #endif    /* NOTYET */
  127.     /* since access and subclass are uniquely determined by the
  128.        above fields already tested, there is no need to check them. */
  129.  
  130.     return TRUE;
  131. }
  132.  
  133.  
  134. /* initItemFields
  135.     Initialize fields for newly allocated gopher items. 
  136.     This is not used for clearing previously used items,
  137.     otherwise it will leak dynamically allocated memory. */
  138.  
  139. void
  140. initItemFields(gi)
  141. gopherItemP    gi;
  142. {
  143.     gi->selector.len = 0;
  144.     gi->selector.data = NULL;
  145.  
  146.     return;
  147. }
  148.  
  149.  
  150. /* getItemIndex
  151.     return an item's index search string */
  152. char    *
  153. getItemIndex(gi)
  154. gopherItemP    gi;
  155. {
  156.     char    *ind;
  157.     char    *string;
  158.  
  159.     if (gi == NULL) return NULL;
  160.  
  161.     string = vStringValue(&(gi->selector));
  162.     ind = index(string, '\t');
  163.  
  164.     if (ind != NULL) ind++;
  165.  
  166.     return ind;
  167.  
  168. }
  169.  
  170.  
  171. #ifdef DEBUG_LIST
  172.  
  173.  
  174. /* printItem
  175.    Print the contents of a gopher item */
  176.  
  177. void
  178. printItem(gi, label)
  179. gopherItemP     gi;
  180. char            *label;
  181. {
  182.     int        i;
  183.  
  184.         if (gi == NULL) fprintf(stdout, "NULL item %s\n", label);
  185.         else {
  186.                 fprintf (stdout, "item %s:\n", label);
  187.         fprintf (stdout, "\t{ type:\t%c\n", gi->type);
  188.         fprintf (stdout, "\t  prefix:\t%*s\n", PREFIX_LEN,
  189.                                USER_STRING_PREFIX(gi));
  190.         fprintf (stdout, "\t  user:\t%s\n", USER_STRING(gi));
  191.         fprintf (stdout, "\t  path:\t(%d chars):%s\n",
  192.                     gi->selector.len, gi->selector.data);
  193.         fprintf (stdout, "\t  host:\t%s\n", gi->host);
  194.         fprintf (stdout, "\t  port:\t%d\n", gi->port);
  195.         fprintf (stdout, "\t  plus:\t%s\n", gi->plus?"True":"False");
  196.         fprintf (stdout, "\t  accessOk:\t%s\n",
  197.                         gi->accessOk?"True":"False");
  198.         fprintf (stdout, "\t}\n");
  199.         }
  200. }
  201. #endif /* DEBUG_LIST */
  202.